<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    #root {
      border: 2px solid pink;
    }

    #box1 {
      background: grey;
      overflow: auto;
    }

    #child {
      margin-bottom: -40px;
      border: 2px solid #000;
    }

    img {
      display: block;
      width: 400px;
      height: 200px;
    }

    #box2 {
      width: 80%;
      height: 100px;
      background: aquamarine;
      border: 2px solid #FFF;
    }
  </style>
</head>

<body>
  <div id="root">
    <div id="box1">
      <div id="child">
        <img src="https://img0.baidu.com/it/u=3399826420,1285863134&fm=26&fmt=auto" alt="">
        <img src="https://img2.baidu.com/it/u=1467000107,2024249069&fm=26&fmt=auto" alt="">
        <img src="https://img2.baidu.com/it/u=3593164165,3970952249&fm=26&fmt=auto" alt="">
        <img src="https://img2.baidu.com/it/u=3141540934,967349315&fm=26&fmt=auto" alt="">
      </div>
    </div>
    <div id="box2">这是box2</div>
    <button id="btn">按钮</button>
  </div>

  <script>


    const btn = document.getElementById('btn')

    

    const target  = document.getElementById('child')


    btn.onclick = function () {
      target.innerHTML += `<img src="https://img2.baidu.com/it/u=3141540934,967349315&fm=26&fmt=auto" alt="123">`
    }

    // 1、提供 解决 box margin-bottom 负值问题 方法
    function handlerBoxHeight(ele) {
      const { clientHeight, scrollHeight, offsetHeight } = ele;
      if (ele.scrollHeight >= ele.clientHeight) {
        // 做逻辑处理
        console.log('逻辑处理！！！');
        ele.style.paddingBottom = scrollHeight - clientHeight + 'px';
        return true;
      } else {
        return false;
      }
    }

    var box1 = document.getElementById('box1');
    window.onload = function () {
      // 2、页面渲染挂载完成后，尝试处理一次
      var isHandler = handlerBoxHeight(box1);

      // 3、如果不满足处理条件（如：数据是接口异步请求后才进行渲染的）
      if (!isHandler) {
        var timer = null;
        timer = setInterval(function () {
          console.log('不断请求！！！')
          var isHandler = handlerBoxHeight(box1);
          // 4、处理完成。
          isHandler && clearInterval(timer);
        }, 100);
      }
    }


  </script>
</body>

</html>